#include "font.h"

cFontMgr g_FontMgr;
cFontMgr* GetFontMgr()
{
	return &g_FontMgr;
}

//font

cDXFont::cDXFont()
{
	wcscpy(m_FontDesc.FaceName,L"Tahoma");
	m_FontDesc.Height = 20;
	m_FontDesc.Width = 7;
	m_FontDesc.Weight = 10;
	m_FontDesc.MipLevels = 0;
	m_FontDesc.Italic = false;
	m_FontDesc.CharSet = DEFAULT_CHARSET;
	m_FontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	m_FontDesc.Quality = DEFAULT_QUALITY;
	m_FontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;

	pDevice = GetDevice9();
}

void cDXFont::SetFontDesc(INT Height,UINT Width,UINT Weight,BOOL Italic,TCHAR* Facename)
{	
	wcscpy(m_FontDesc.FaceName,Facename);
	m_FontDesc.Height = Height;
	m_FontDesc.Width = Width;
	m_FontDesc.Weight = Weight;
	m_FontDesc.Italic = Italic;
	m_FontDesc.CharSet = DEFAULT_CHARSET;
	m_FontDesc.MipLevels = 0;
	m_FontDesc.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	m_FontDesc.Quality = DEFAULT_QUALITY;
	m_FontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
}

void cDXFont::Create()
{
	HR(D3DXCreateFontIndirect(pDevice,&m_FontDesc,&m_pFont))
}

void cDXFont::DrawText(LPCWSTR pString,LPRECT pRect,DWORD Format,D3DCOLOR Color)
{
	m_pFont->DrawTextW(NULL,pString,-1,pRect,Format,Color);
}

void cDXFont::Release()
{
	SAFE_RELEASE(m_pFont);
}

//fontmgr

void cFontMgr::PushFont(string name,cDXFont* font)
{
	if(mapFont.find(name)==mapFont.end()){
		mapFont.insert(make_pair(name,font));
	}
}

cDXFont* cFontMgr::GetFont(string name)
{
	if(mapFont.find(name)!=mapFont.end())
	{
		return mapFont[name];
	}
	ERRBOX("No this Font!");
	return NULL;
}

void cFontMgr::Release()
{
	auto itr = mapFont.begin();
	while(itr!=mapFont.end()){
		itr->second->Release();
		++itr;
	}
	mapFont.clear();
}

